require "net/http"
require "mysql2"
require "rexml/document"

class People
	attr_accessor :id,:title,:location_id,:location,:content,:signature,:self_link,:alternate_link,:icon_link
end

mysql_client=Mysql2::Client.new(:host => "127.0.0.1", :username => "dev",:password=>"justsoso123",:database=>"xdouban")


user_id_list = mysql_client.query("select * from new_users order by id asc limit 2000")

user_id_list.each() do |row|

p = row["user_id"]
row_id = row["id"] 

puts p 


begin

xml=Net::HTTP.get(URI.parse("http://api.douban.com/people/#{p.chop}?apikey=09ebbaa767412e021dc32ff635c38a06"));


doc=REXML::Document.new(xml)

entry=doc.elements['entry']

p=People.new

puts "id"
puts entry.elements["id"].text
p.id=entry.elements["id"].text

puts "title"
#puts entry.elements["title"].text
#p.title=entry.elements["title"].text


puts "loc"
puts entry.elements["db:location"]

if entry.elements["db:location"]
	puts entry.elements["db:location"].text
	p.location=entry.elements["db:location"].text

	puts entry.elements["db:location"].attributes["id"]
	p.location_id=entry.elements["db:location"].attributes["id"]
else
	p.location=''
	p.location_id=''
end

puts entry.elements["content"].text
p.content=entry.elements["content"].text

puts entry.elements["db:signature"].text
p.signature=entry.elements["db:signature"].text

entry.elements.each("link") do |el|
	if el.attributes['rel'] == 'self'
		p.self_link=el.attributes['href']
	elsif el.attributes['rel'] == 'alternate'
		p.alternate_link=el.attributes['href']
	elsif el.attributes['rel'] == 'icon'
		p.icon_link=el.attributes['href']
	end

	puts el.attributes['href']
end


content=''

if p.content
	content=mysql_client.escape(p.content)
end

mysql_client.query("delete from new_users where id=#{row_id}")

rescue Exception
	puts "Excetpion"
    puts( $!.class )
    puts( $! )
end

sleep 2

end






